package org.example.new_hbps_backend_reconstruction.config;

import jakarta.annotation.Resource;
import org.example.new_hbps_backend_reconstruction.interceptor.LoginInterceptor;
import org.example.new_hbps_backend_reconstruction.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 丁利军
 * @date 2025年10月10日 13:30
 * @description spring mvc相关配置
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/swagger-ui/**",
                        "/doc.html",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/favicon.ico",
                        "/error",
                        "/patient/patientDeviceV2/generateQRCodeString",
                        "/patient/patientDeviceV2/callback",
                        //"/patientDeviceV2/**",
                        //"/patient/**",
                        //"/heartRateLogs/get_newly_heart_rate_data/**",
                        "/js/**", "/css/**",
                        //"/patient/**"
                        "/api/token/**"

                )
                .order(0);

        // 登录校验拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/swagger-ui/**",
                        "/doc.html",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/favicon.ico",
                        "/error",
                        "/ws-chat/**",
                        "/js/**", "/css/**",
                        "/patient/patientLogin/get_wx_open_id/**",
                        "/patient/patientLogin/get_phone_number",
                        "/patient/patientLogin/bind_phone_number",
                        "/patient/patientLogin/custom_login",
                        "/patient/patientLogin/normal_test",
                        "/patient/patientDeviceV2/generateQRCodeString",
                        "/patient/patientDeviceV2/callback",
                        "/auth/**",
                        //"/api/oss/**",
                       // "/patientDeviceV2/**",
                       // "/heartRateLogs/get_newly_heart_rate_data/**",
                      //  "/patient/**"
                        "/api/token/**"
                )
                .order(1);
    }
    // 跨域拦截器
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                //  Spring Boot 3 推荐写法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Content-Disposition")
                .allowCredentials(true)
                //  如果你前端需要带 cookie/token 等
                .maxAge(3600);
    }
}

